package com.fourforfo.fourmall.product.service.impl;

import com.fourforfo.fourmall.product.vo.AttrGroupRelationVO;
import com.fourforfo.fourmall.product.vo.ProductItemVO;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.fourforfo.common.utils.PageUtils;
import com.fourforfo.common.utils.Query;

import com.fourforfo.fourmall.product.dao.AttrAttrgroupRelationDao;
import com.fourforfo.fourmall.product.entity.AttrAttrgroupRelationEntity;
import com.fourforfo.fourmall.product.service.AttrAttrgroupRelationService;


@Service("attrAttrgroupRelationService")
public class AttrAttrgroupRelationServiceImpl extends ServiceImpl<AttrAttrgroupRelationDao, AttrAttrgroupRelationEntity> implements AttrAttrgroupRelationService {

    @Override
    public PageUtils queryPage(Map<String, Object> params) {
        IPage<AttrAttrgroupRelationEntity> page = this.page(
                new Query<AttrAttrgroupRelationEntity>().getPage(params),
                new QueryWrapper<AttrAttrgroupRelationEntity>()
        );

        return new PageUtils(page);
    }

    @Override
    public void removeByAttrAndGroup(List<AttrAttrgroupRelationEntity> entities) {

        baseMapper.deleteBatchRelation(entities);
//        this.remove(new QueryWrapper<>().eq("attr_id",).eq("attr_group_id"));

    }

    /**
     * 根据attrIds删除所有attrattrGroupRelation信息
     * @param attrIds
     */
    @Override
    public void removeByAttrIds(List<Long> attrIds) {
        baseMapper.deleteByAttrIds(attrIds);
    }

    @Override
    public void removeByAttrGroupIds(List<Long> attrGroupIds) {
        baseMapper.deleteByAttrGroupIds(attrGroupIds);
    }

    /**
     * 根据spuId获取attrId，attrName，attrValue，attrGroupId，attrGroupName
     * 1.根据spuId从pms_product_attr_value表中获取所有attrId属性
     *   SELECT attr_id FROM pms_product_attr_value AS e WHERE e.spu_id=13 AND e.attr_id=d.attr_id
     *
     * 2.根据第1步获取的attrIds从pms_attr_attrgroup_relation表中查出所有attr_group_id并按attr_group_id分组
     *   SELECT d.attr_group_id FROM pms_attr_attrgroup_relation AS d
     *     WHERE EXISTS(第1步的SQL) AND d.attr_group_id=c.attr_group_id GROUP BY c.attr_group_id
     *
     * 3.根据第2步获取的attr_group_id从表pms_attr_group 查出所有符合条件的attr_group_id和attr_group_name属性
     *   SELECT c.attr_group_id,c.attr_group_name from pms_attr_group AS c WHERE EXISTS(第2步的SQL)
     *
     * 4.将表pms_attr_attrgroup_relation 与第3步获取的attr_group_id和attr_group_name数据根据attr_group_id进行left join操作
     *   SELECT a.attr_id,a.attr_group_id,b.attr_group_name
     *     FROM pms_attr_attrgroup_relation AS a
     *     LEFT JOIN ( [第3步的SQL] ) AS b ON a.attr_group_id=b.attr_group_id;
     *
     * 5.将表`pms_attr_attrgroup_relation` 与第4步获取的`attr_id`，`attr_group_id`和`attr_group_name`数据根据`attr_id`进行`left join`操作
     *   SELECT x.attr_id,x.attr_name,x.attr_value,y.attr_group_id,y.attr_group_name
     * 	   FROM pms_product_attr_value AS x
     * 	   LEFT JOIN (第4步的SQL) AS y
     *     ON x.attr_id=y.attr_id;
     */
    @Override
    public List<ProductItemVO.SpuItemBaseAttrVO> listAttrGroupAndAttr(Long spuId) {
        List<ProductItemVO.SpuItemBaseAttrVO> spuItemBaseAttrVOS = baseMapper.listAttrGroupAndAttr(spuId);
        return spuItemBaseAttrVOS;
    }

    @Override
    public void saveBatch(List<AttrGroupRelationVO> relationVOs) {
        List<AttrAttrgroupRelationEntity> relationEntities = relationVOs.stream().map((item)->{
            AttrAttrgroupRelationEntity attrAttrgroupRelationEntity = new AttrAttrgroupRelationEntity();
            BeanUtils.copyProperties(item,attrAttrgroupRelationEntity);
            return attrAttrgroupRelationEntity;
        }).collect(Collectors.toList());
        this.saveBatch(relationEntities);
    }



}